package com.draven.tongzhou.controller

import javax.servlet.http.HttpServletRequest
import org.springframework.web.servlet.ModelAndView

/**
  * Created by Draven on 2018/7/9.
  * Email:hjx@qiandw.com
  */
class CommonController {

  def toPage(path: String) = new ModelAndView(path)

  def toPageWithAttr(path: String, attrs: Map[String, Object]) = {
    val modelAndView = new ModelAndView(path)
    attrs.foreach(keyAndVal => {
      modelAndView.addObject(keyAndVal._1, keyAndVal._2)
    })
    modelAndView
  }

  def getIp(request: HttpServletRequest): String = {
    var ip = request.getHeader("x-forwarded-for")
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("Proxy-Client-IP")
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("WL-Proxy-Client-IP")
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("HTTP_CLIENT_IP")
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("HTTP_X_FORWARDED_FOR")
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getRemoteAddr
    }
    ip
  }
}
